package com.hyh.shiyanshiguanli.controller;

import com.hyh.shiyanshiguanli.constant.JwtClaimsConstant;
import com.hyh.shiyanshiguanli.constant.MessageConstant;
import com.hyh.shiyanshiguanli.domain.dto.*;
import com.hyh.shiyanshiguanli.domain.entity.Admin;
import com.hyh.shiyanshiguanli.domain.entity.Student;
import com.hyh.shiyanshiguanli.domain.result.Result;
import com.hyh.shiyanshiguanli.domain.vo.AdminVO;
import com.hyh.shiyanshiguanli.domain.vo.LoginVO;
import com.hyh.shiyanshiguanli.domain.vo.StudentVO;
import com.hyh.shiyanshiguanli.properties.JwtProperties;
import com.hyh.shiyanshiguanli.service.AdminService;
import com.hyh.shiyanshiguanli.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @author hyh
 * @version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/admin")
public class adminController {

    @Autowired
    AdminService adminService;
    @Autowired
    private JwtProperties jwtProperties;

    @PostMapping("/login")
    public Result<LoginVO> Login(@RequestBody Admin adm) {
        log.info("管理员登录{}", adm);
        Admin admin = adminService.login(adm);
        //登录成功生成JWT令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.ADMIN_ID, admin.getId());
        claims.put(JwtClaimsConstant.MARK,2);
        //根据密钥ttl和自定义载荷来得到token
        String token = JwtUtil.createJWT(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

        LoginVO loginVO = LoginVO.builder()
                .id(admin.getId())
                .name(admin.getUsername())
                .image(admin.getImage())
                .token(token)
                .build();
        log.info("{}",loginVO);
        return Result.success(loginVO);
    }

    @GetMapping("/page")
    public Result<AdminVO> page(MessageDTO messageDTO){
        log.info("管理员分页查询{}",adminService);
        AdminVO adminVO = adminService.page(messageDTO);
        return Result.success(adminVO);
    }


    @PostMapping("/add")
    public Result addPublicInformation(@RequestBody AdminAddDTO adminAddDTO){
        log.info("添加adminAddDTO{}",adminAddDTO);
        adminService.add(adminAddDTO);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Admin> selectById(@PathVariable Integer id){
        log.info("根据id查询Admin{}",id);
        Admin admin = adminService.selectById(id);
        return Result.success(admin);
    }

    @PutMapping("/update")
    public Result updateById(@RequestBody AdminUpdateDTO adminUpdateDTO){
        log.info("更新系统管理员数据，id为{}",adminUpdateDTO.getId());
        adminService.updateById(adminUpdateDTO);
        return Result.success();
    }

    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id){
        log.info("删除id为{}的libManager",id);
        int delete = adminService.deleteById(id);
        if (delete == MessageConstant.SUCCESS){
            return Result.success();
        }
        else{
            return Result.error("不能删除");
        }

    }

}
